package design.graduation.novelwebsite.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import design.graduation.novelwebsite.constant.Constant;
import design.graduation.novelwebsite.model.dto.ResultPageJson;
import design.graduation.novelwebsite.model.entity.IncludeBook;
import design.graduation.novelwebsite.model.vo.IncludeBookVo;
import design.graduation.novelwebsite.service.IncludeBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 洪少尉
 * @Date: 2020/3/23 22:16
 * @Description: 小说分类
 */
@RestController
@CrossOrigin
@RequestMapping("sort")
public class SortController {
	@Autowired
	private IncludeBookService includeBookService;
	/**
	 * 获取一级分类的小说列表
	 */
	@RequestMapping("primaryClassification/{primaryClassification}")
	public ResultPageJson primaryClassification(IncludeBookVo includeBookVo){
		return getResultJson(includeBookVo, IncludeBook.COL_PRIMARY_CLASSIFICATION,includeBookVo.getPrimaryClassification());
	}
	/**
	 * 获取二级分类的小说列表
	 */
	@RequestMapping("secondaryClassification/{secondaryClassification}")
	public ResultPageJson secondaryClassification(IncludeBookVo includeBookVo){
		return getResultJson(includeBookVo, IncludeBook.COL_SECONDARY_CLASSIFICATION, includeBookVo.getSecondaryClassification());
	}
	/**
	 * 获取三级分类的小说列表
	 */
	@RequestMapping("tertiaryClassification/{tertiaryClassification}")
	public ResultPageJson tertiaryClassification(IncludeBookVo includeBookVo){
		return getResultJson(includeBookVo,IncludeBook.COL_TERTIARY_CLASSIFICATION,includeBookVo.getTertiaryClassification());
	}
	/**
	 * 根据作者查找
	 * @return
	 */
	@RequestMapping("author/{author}")
	public ResultPageJson author(IncludeBookVo includeBookVo){
		return getResultJson(includeBookVo,IncludeBook.COL_AUTHOR,includeBookVo.getAuthor());
	}
	@RequestMapping("search")
	public Object search(String keyword){
		Page<IncludeBook> page = new Page<>(1,15);
		QueryWrapper<IncludeBook> queryWrapper = new QueryWrapper<>();
		queryWrapper.like(IncludeBook.COL_BOOK_NAME,keyword)
				.or().like(IncludeBook.COL_AUTHOR,keyword);
		this.includeBookService.page(page,queryWrapper);
		return new ResultPageJson(page.getTotal(),page.getCurrent(),page.getPages(),page.getRecords());
	}

	private ResultPageJson getResultJson(IncludeBookVo includeBookVo, String databaseField, String entityField) {
		Page<IncludeBook> page = new Page<>(includeBookVo.getPage(),includeBookVo.getLimit());
		QueryWrapper<IncludeBook> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(databaseField, entityField)
				.eq(IncludeBook.COL_STATUS, Constant.STATUS_TRUE)
				//根据id降序排列
				.orderByDesc(IncludeBook.COL_CREATE_TIME);
		this.includeBookService.page(page, queryWrapper);
		return new ResultPageJson(page.getTotal(),page.getCurrent(),page.getPages(),page.getRecords());
	}

	private ResultPageJson getResultJson(IncludeBookVo includeBookVo, String databaseField, Integer entityField) {
		Page<IncludeBook> page = new Page<>(includeBookVo.getPage(),includeBookVo.getLimit());
		QueryWrapper<IncludeBook> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(databaseField, entityField)
				.eq(IncludeBook.COL_STATUS, Constant.STATUS_TRUE)
				//根据时间降序排列
				.orderByDesc(IncludeBook.COL_CREATE_TIME);
		this.includeBookService.page(page,queryWrapper);
		return new ResultPageJson(page.getTotal(),page.getCurrent(),page.getPages(),page.getRecords());
	}
}
